﻿using System;
using System.Linq;
using System.Collections.Generic;
using T.FAS.Runtime.Metadata.SPI;
using T.FAS.Runtime.Metadata.Entity;
using System.Configuration;
using Newtonsoft.Json;

namespace T.FAS.Runtime.DataModel.Dao
{
    /// <summary>
    /// 配置工具
    /// </summary>
    public class ConfigTool
    {
        private static Dictionary<string, string> s_configCache = new Dictionary<string, string>();
        private static readonly object s_lockObj = new object();
        private const string _debugModeKey = "DebugMode";
        private static bool s_debugMode;
        private static bool s_isDebugAll = false;
        
        /// <summary>
        /// 是否调试模式，true:是 false:否
        /// </summary>
        public static bool DebugMode
        {
            get
            {
                return s_debugMode;
            }
        }

        private static List<string> _debugMeta = new List<string>();
        public static List<string> DebugMeta
        {
            get
            {
                return _debugMeta;
            }
        }

        static ConfigTool()
        {
            string _debugModeStr = GetConfig(_debugModeKey);
            if (string.IsNullOrEmpty(_debugModeStr))
                s_debugMode = false;
            else
            {
                try
                {
                    if (_debugModeStr.ToLower() == "true")
                    {
                        s_debugMode = true;
                        s_isDebugAll = true;
                    }
                    else
                    {
                        DebugModeModel mode = JsonConvert.DeserializeObject<DebugModeModel>(_debugModeStr);
                        s_debugMode = mode.Flag;
                        if (mode.MetaType != null && mode.MetaType.Count > 0)
                            _debugMeta = mode.MetaType;
                    }
                }
                catch
                {
                    s_debugMode = false;
                }
            }
        }

        private static string GetConfig(string key)
        {

            if (!s_configCache.ContainsKey(key))
            {
                lock (s_lockObj)
                {
                    if (!s_configCache.ContainsKey(key))
                    {
                        string value;
                        try
                        {
                            value = ConfigurationManager.AppSettings.Get(key);
                        }
                        catch
                        {
                            value = null;
                        }
                        s_configCache[key] = value;
                    }
                }
            }
            return s_configCache[key];
        }

        /// <summary>
        /// 是否启用调试模式
        /// </summary>
        /// <param name="metaTypeID">元数据类型</param>
        /// <returns>true:调试模式，false:非调试模式</returns>
        public static bool IsDebug(string metaTypeID)
        {
            if (string.IsNullOrEmpty(metaTypeID))
                return false;
            return DebugMode && (s_isDebugAll || DebugMeta.Contains(metaTypeID));
        }

        class DebugModeModel
        {
            [JsonProperty("flag")]
            public bool Flag { get; set; }
            [JsonProperty("meta")]
            public List<string> MetaType { get; set; }
        }
    }
}
